/*
 * DERECHOS DE AUTOR (C) 2013 POR Jose Carlos Palma.
 *
 * TODOS LOS DERECHOS RESERVADOS. NINGUNA PARTE DE  ESTE CODIGO FUENTE 
 * PUEDE SER REPRODUCIDA O TRANSMITIDA O UTILIZADA EN  CUALQUIER FORMA 
 * O POR CUALQUIER  MEDIO, YA SEA  ELECTRÓNICO O MECÁNICO,  INCLUYENDO 
 * EL FOTOCOPIADO, GRABACIÓN O POR CUALQUIER SISTEMA DE ALMACENAMIENTO 
 * O RECUPERACIÓN DE INFORMACIÓN  SIN EL PERMISO PREVIO Y  POR ESCRITO
 * DEL PROPIETARIO DE LOS DERECHOS DE AUTOR.
 */
package org.jss;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.hsqldb.server.ServerAcl;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;

/**
 *
 * @author Jose Carlos Palma
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, ServerAcl.AclFormatException {
        // TODO code application logic here

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        textEncryptor.setPassword("jobscheduler");

        String myEncryptedText = textEncryptor.encrypt("jssdb1234$");
        
        System.out.println(myEncryptedText);

        BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
        String encryptedPassword = passwordEncryptor.encryptPassword("jssdb1234$");

        System.out.println(encryptedPassword);



        /*
         * First, create (or ask some other component for) the adequate encryptor for
         * decrypting the values in our .properties file.
         */
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("jobscheduler"); // could be got from web, env variable...

        /*
         * Create our EncryptableProperties object and load it the usual way.
         */
        Properties props = new EncryptableProperties(encryptor);
        props.load(new FileInputStream("jssapp.conf"));

        /*
         * To get a non-encrypted value, we just get it with getProperty...
         */
        String user = props.getProperty("db.user");
        System.out.println(user);

        /*
         * ...and to get an encrypted value, we do exactly the same. Decryption will
         * be transparently performed behind the scenes.
         */
        String pass = props.getProperty("db.password");
        System.out.println(pass);
        //props.setProperty("db.password", "jsspass1234$");

        //props.store(new FileOutputStream("jssapp.conf"), "descripcion");

        /*
         HsqlProperties prop = new HsqlProperties("db/jssdb.conf");
        
         Server jssdb = new Server();
         jssdb.setProperties(prop);
        
         jssdb.start();
         */

    }
}
